<?php

namespace app\user\logic;

use app\common\exception\LogicException;
use app\model\Barn;
use app\model\Factory;
use app\model\Fence;
use app\model\Pig;

class FactoryLogic
{
    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2023/08/26 17:32:59
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param)
    {
        $fenceModel = new Factory();
        $query = $fenceModel -> alias('f')
            ->field("f.id,f.name,f.contacts,f.phone,f.address,f.create_time,f.update_time")
            ->where('f.delete_time',$fenceModel::IS_DELETE_NO);
        if ($param['name'] != '') {
            $query->where("f.name","like","%".trim($param['name'])."%");
        }
        if ($param['contacts'] ?? "") {
            $query->where("f.contacts","like","%".trim($param['contacts'])."%");
        }
        if ($param['phone'] ?? "") {
            $query->where("f.phone",$param['phone']);
        }

        return $query->order("f.id DESC")
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 猪厂详情
     * @author: zj
     * @param int $id
     * @time 2023/08/29 15:53:04
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $id)
    {
        $factoryModel = new Factory();
        $query = $factoryModel -> alias('f')
            ->field('f.id,f.name,f.contacts,f.phone,f.address,f.create_time,f.update_time')
            ->where('id',$id)
            ->where('f.delete_time',$factoryModel::IS_DELETE_NO);

        $info = $query->find();
        if (!$info) {
            throw new LogicException("数据不存在");
        }
        $info = $info->toArray();
        
        return $info;
    }

    /**
     * @Description 详情手机端数据展示补充
     * @author: zj
     * @param int $factoryId
     * @time 2024/03/19 10:23:15
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function detailSupply(int $factoryId)
    {
        // 统计猪栋数量
        $barnModel = new Barn();
        $barnTotal = $barnModel->checkBarnCount($factoryId);

        // 统计猪栏数量
        $fenceModel = new Fence();
        $fenceTotal = $fenceModel->checkFenceCount($factoryId);

        // 统计猪的数量
        $pigModel = new Pig();
        // 全部猪
        $all = $pigModel->checkPigCount($factoryId);
        // 健康猪数量
        $health = $pigModel->checkPigCount($factoryId,0,0,$pigModel::STATUS_HEALTH);
        // 疑似健康
        $mayBeHealth = $pigModel->checkPigCount($factoryId,0,0,$pigModel::STATUS_POTENTIAL_HEALTH);
        // 生病
        $sick = $pigModel->checkPigCount($factoryId,0,0,$pigModel::STATUS_SICK);
        // 已处理，待观察
        $treat = $pigModel->checkPigCount($factoryId,0,0,$pigModel::STATUS_OBSERVATION);
        // 治疗无效
        $treatmentIneffective = $pigModel->checkPigCount($factoryId,0,0,$pigModel::STATUS_INEFFECTIVE);

        return [
            'building_total' => $barnTotal,
            'fence_total' => $fenceTotal,
            'all_husbandry' => $all,
            'health_num' => $health,
            'suspectedhealthy_num' => $mayBeHealth,
            'sick_num' => $sick,
            'treat_num' => $treat,
            'treat_ineffective_num' => $treatmentIneffective,
        ];
    }

    /**
     * @Description 猪厂的栋、猪栏、猪的统计
     * @author: zj
     * @param int $factoryId
     * @time 2023/09/04 16:36:25
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function statistic(int $factoryId)
    {
        // 猪厂栋的数量
        $barnModel = new Barn();
        $fenceModel = new Fence();
        // 全部 栋 数量
        $allBarnCount = $barnModel->checkBarnCount($factoryId);
        $allFenceCount = 0;// 总栏数
        // 全部栋的列表
        $barnList = $barnModel->getAllList($factoryId);
        if ($barnList) {
            foreach ($barnList as $key => &$value) {
                // 每栋的猪栏数
                $fenceCount = $fenceModel->checkFenceCount($factoryId,$value['id']);
                $value['count'] = $fenceCount;
                $allFenceCount += $fenceCount;
            }
            unset($value);
        }
        // 猪厂 全部未出栏的猪数量
        $pigModel = new Pig();
        $allPigCount = $pigModel->checkPigCount($factoryId);

        return [
            'all_building_num' => $allBarnCount,
            'building_list' => $barnList,
            'all_fence_num' => $allFenceCount,
            'all_pig_num' => $allPigCount
        ];
    }

    /**
     * @Description 下拉列表
     * @author: zj
     * @param array $param
     * @time 2023/08/26 17:32:37
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function selectList(array $param)
    {
        $factoryModel = new Factory();
        $query = $factoryModel -> field("id,name")
            ->where('id','>',$param['last_id'])
            ->where("delete_time",$factoryModel::IS_DELETE_NO);

        if ($param['name'] ?? "") {
            $query->where("name","LIKE","%".trim($param['name'])."%");
        }

        return $query->order("id","ASC")
            ->limit($param['limit'])
            ->select()
            ->toArray();
    }
}